package modulesAPI.v2.composite;

import java.util.GregorianCalendar;

import com.caterina.components.CPanel;
import com.caterina.resources.CModules;
import com.caterina.resources.CWords;

import person.Customer;
import person.Customer.Type;
import person.Person;
import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModComboBox;
import modulesAPI.v2.basics.ModTextField;

public class MCustomer extends Module<Customer> {
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un cliente
	 * */
	public MCustomer(Module<?> ownerModule, Customer content, String tag,String description, ViewType viewType,int...options) {
		super(ownerModule, content, tag, description, viewType,options);		
		createModules();
		this.addModules(custType,person,vat);
	}
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un cliente
	 * */
	public MCustomer(Module<?> ownerModule, String tag,String description, ViewType viewType) {
		this(ownerModule, new Customer("", "", "", "", "", "", "", Customer.Type.PERSON, "",new GregorianCalendar()), tag, description, viewType);
	}
	private void createModules(){
		if (this.isOptionEnabled(VIEW_LESS))this.createViewLessModules();
		else if(this.isOptionEnabled(VIEW_MORE_LESS))this.createViewMoreLessModules();
		else this.createStandardView();
	}
	private void createStandardView(){
		this.person=new MPerson(this, this.getContent(), "CUSTOMER_DETAILS", "CUSTOMER_DETAIL_DESC", this.getViewType());
		this.custType=new ModComboBox(this, this.getContent().getType(), "CUSTOMER_TYPE", "CUSTOMER_TYPE_DESC", this.getViewType(), Customer.Type.values());
		this.vat=new ModTextField(this, this.getContent().getVAT(), "CUSTOMER_VAT", "CUSTOMER_VAT_DESC",this.getViewType());
		if (custType.getContent().equals(Customer.Type.PERSON))
			this.vat.setVisible(false);
	}
	private void createViewLessModules(){
		this.person=new MPerson(this, this.getContent(), "CUSTOMER_DETAILS", "CUSTOMER_DETAIL_DESC", this.getViewType(),MPerson.VIEW_LESS);
		this.custType=new ModComboBox(this, this.getContent().getType(), "CUSTOMER_TYPE", "CUSTOMER_TYPE_DESC", this.getViewType(), Customer.Type.values());
		this.vat=new ModTextField(this, this.getContent().getVAT(), "CUSTOMER_VAT", "CUSTOMER_VAT_DESC",ViewType.HIDDEN);
		if (custType.getContent().equals(Customer.Type.PERSON))
			this.vat.setVisible(false);
	}
	private void createViewMoreLessModules(){
		this.person=new MPerson(this, this.getContent(), "CUSTOMER_DETAILS", "CUSTOMER_DETAIL_DESC", this.getViewType(),MPerson.VIEW_MORE_LESS);
		this.custType=new ModComboBox(this, this.getContent().getType(), "CUSTOMER_TYPE", "CUSTOMER_TYPE_DESC", ViewType.HIDDEN, Customer.Type.values());
		this.vat=new ModTextField(this, this.getContent().getVAT(), "CUSTOMER_VAT", "CUSTOMER_VAT_DESC",ViewType.HIDDEN);
		this.vat.setVisible(false);
		if (custType.getContent().equals(Customer.Type.PERSON))
			this.vat.setVisible(false);
	}
	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	@Override
	public CPanel getInListPanel(boolean isSelected) {
		return CModules.formatForList(this.getContent().getName()+"  "+this.getContent().getSurname(), 
				CWords.getWord(this.getContent().getType().name()), isSelected);
	}
	protected void beforeFireChange(Module<?> mod){
		this.setContent(new Customer(
				this.getContent().getId(), 
				person.getContent(),
				(Type) custType.getContent(), 
				vat.getContent()));
		if (custType.getContent().equals(Customer.Type.PERSON)) this.vat.setVisible(false);
		else this.vat.setVisible(true);
	}
	@Override
	protected void afterFireChange(Module<?> mod) {
		
	}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		return null;
	}
	public static final int VIEW_LESS=0;
	public static final int VIEW_MORE_LESS=1;
	private Module<Person> person;
	private Module<Enum<?>> custType;
	private Module<String> vat;

}
